package com.yyk.yykywd.ui.work;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.base.BaseRecyclerAdapter;
import com.yyk.yykywd.base.BaseRecyclerHolder;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.WorkPactlistInfo;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.widght.diver.CustomDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 合同列表
 */
public class WorkPactlistActivity extends BaseActivity {

    @BindView(R.id.ll_emptyview)
    LinearLayout llEmptyview;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    private BaseRecyclerAdapter<WorkPactlistInfo.DataBean> adapter;
    private List<WorkPactlistInfo.DataBean> list = new ArrayList<>();


    @Override
    public int getLayoutId() {
        return R.layout.activity_work_pactlist;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("合同列表");
    }


    @Override
    public void initData() {
        initAdapter();
        postWorkPactlist(true);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        postWorkPactlist(true);
    }

//    private int pageIndex;

    private void initAdapter() {

        adapter = new BaseRecyclerAdapter<WorkPactlistInfo.DataBean>(mActivity, list, R.layout.adapter_item_workpactlist) {
            @Override
            public void convert(BaseRecyclerHolder holder, WorkPactlistInfo.DataBean item, int position, boolean isScrolling) {
                TextView tv = holder.getView(R.id.tv_state);
                if (item.getState() == 5) {
                    holder.setText(R.id.tv_state, "合作成功");
                    tv.setBackground(getResources().getDrawable(R.drawable.bg_green_fang));
                    tv.setTextColor(getResources().getColor(R.color.green_2a));
                } else {
                    holder.setText(R.id.tv_state, "待回款");
                    tv.setBackground(getResources().getDrawable(R.drawable.bg_red_fang));
                    tv.setTextColor(getResources().getColor(R.color.red_e8));
                }
                holder.setText(R.id.tv_topname, item.getTopname());
                holder.setText(R.id.tv_hname, "目标机构：" + item.getHname());
                holder.setText(R.id.tv_ftime, "上传时间：" + item.getCreatetime());

            }
        };
        rvList.addItemDecoration(new CustomDecoration(mActivity, CustomDecoration.VERTICAL_LIST, R.drawable.divider_f8f_10, 0));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        rvList.setLayoutManager(linearLayoutManager);
        rvList.setAdapter(adapter);
        /**
         * TODO 分页加载   由于接口原因没有请求 数量参数，先作罢以后再补
         */
//        rvList.addOnScrollListener(new EndLessOnScrollListener(linearLayoutManager) {
//            @Override
//            public void onLoadMore(int currentPage) {
////                if (pageIndex == currentPage) {
////                    return;
////                }
//                new Handler().postDelayed(new Runnable() {
//                    public void run() {
//                        //请求网络
//                        pageIndex++;
//                        postWorkPactlist(false);
//
//                    }
//                }, 1000);
//            }
//        });
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", list.get(position).getId() + "");
                startAct(QueryWorkPactActivity.class, bundle);
            }
        });

    }

    /**
     * 接口26：合同列表 no（whm）
     */
    private void postWorkPactlist(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid")+"");

        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postWorkPactlist(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<WorkPactlistInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WorkPactlistInfo info) {
                        Logger.e("合同列表" + info.toString());
                        if (info.getCode() == 1) {
                            llEmptyview.setVisibility(View.GONE);
                            rvList.setVisibility(View.VISIBLE);
                            if (flag) {
                                list.clear();
                            }
                            if (info.getData() != null & info.getData().size() > 0) {
                                list.addAll(info.getData());
                            }
                            adapter.notifyDataSetChanged();
                        } else if (info.getCode() == 2) {
                            llEmptyview.setVisibility(View.VISIBLE);
                            rvList.setVisibility(View.GONE);

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
